package cn.uikit.demo.main

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import androidx.recyclerview.widget.GridLayoutManager
import cn.uikit.demo.databinding.ActivityMainBinding
import cn.uikit.demo.databinding.ItemMainBinding
import cn.uikit.demo.dialog.DialogTestActivity
import cn.uikit.demo.location.LocationTestActivity
import cn.uikit.demo.other.OtherTestActivity
import cn.uikit.demo.recyclerview.RvTestActivity
import cn.uikit.demo.vp.ViewPagerTestActivity
import cn.uikit.demo.wheel.WheelActivity
import cn.uikit.demo.widget.CustomViewTestActivity
import sivin.uikit.adapter2.SimpleAdapter
import sivin.uikit.framework.BindingActivity


class MainActivity : BindingActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {

    data class RvItem(val title: String, val pageLauncher: (Context) -> Unit)

    private val dataList = arrayListOf(
        RvItem("杂项点击测试", OtherTestActivity::launch),
        RvItem("ViewPager测试", ViewPagerTestActivity::launch),
        RvItem("Dialog测试", DialogTestActivity::launch),
        RvItem("RecyclerView测试", RvTestActivity::launch),
        RvItem("自定义View测试", CustomViewTestActivity::launch),
        RvItem("定位服务测试", LocationTestActivity::launch),
        RvItem("Wheel测试", WheelActivity::launch)
    )

    private val rvAdapter by lazy {
        SimpleAdapter<RvItem, ItemMainBinding>(ItemMainBinding::inflate) { holder, item, _ ->
            holder.binding.title.text = item.title
            holder.binding.root.setOnClickListener {
                item.pageLauncher(this@MainActivity)
            }
        }
    }

    override fun onViewCreate(savedInstanceState: Bundle?) {
        super.onViewCreate(savedInstanceState)
        binding.rv.apply {
            adapter = rvAdapter
            layoutManager = GridLayoutManager(context, 2)
        }
        rvAdapter.setDataList(dataList)
        binding.root.viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                binding.root.viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        })
    }

    override fun onStop() {
        super.onStop()
        Log.d("Sivin", "onStop: ")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d("Sivin", "onSaveInstanceState: ")
    }
}